package ch.uzh.ifi.seal.ssqa.ui.views;

import java.util.List;

import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;

import ch.uzh.ifi.seal.ssqa.metrics.commands.ApplyHeatMapCommand;
import ch.uzh.ifi.seal.ssqa.metrics.heatmap.CyclomaticComplexityHeatMap;
import ch.uzh.ifi.seal.ssqa.metrics.heatmap.IHeatMap;
import ch.uzh.ifi.seal.ssqa.metrics.heatmap.IssueTypeHeatMap;
import ch.uzh.ifi.seal.ssqa.metrics.heatmap.LinesOfCodeHeatMap;
import ch.uzh.ifi.seal.ssqa.model.Diagram;
import ch.uzh.ifi.seal.ssqa.model.DiagramComponent;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramEditPart;
import ch.uzh.ifi.seal.ssqa.util.DiagramComponentProvider;

public class MetricSelectionView extends ViewPart{

	public static final String ID = "ch.uzh.ifi.seal.ssqa.ui.views.MetricSelectionView";
	
	/* FIXME add this to preference pages */
	public static final String ERROR_INTENSITY = "Error Intensity";
	public static final String CHANGE_COUPLING = "Change Coupling";
	public static final String LINES_OF_CODE = "Lines Of Code";
	public static final String CYCLOMATIC_CODE_COMPLEXITY = "Cyclomatic Code Complexity";
	public static final String FAN_IN = "Fan In";
	public static final String FAN_OUT = "Fan Out";
	
	
	private TableViewer viewer;
	
	Shell shell;

	@Override
	public void createPartControl(Composite parent) {
		this.shell = parent.getShell();
		this.viewer = new TableViewer(parent);
		this.viewer.add(MetricSelectionView.ERROR_INTENSITY);
		this.viewer.add(MetricSelectionView.CHANGE_COUPLING);
		this.viewer.add(MetricSelectionView.LINES_OF_CODE);
		this.viewer.add(MetricSelectionView.CYCLOMATIC_CODE_COMPLEXITY);
		//this.viewer.add(MetricSelectionView.FAN_IN);
		//this.viewer.add(MetricSelectionView.FAN_OUT);
		this.viewer.addDoubleClickListener(new HeatMetricDoubleClickListener());
	}

	@Override
	public void setFocus() {}
	
	public class HeatMetricDoubleClickListener implements IDoubleClickListener{

		@Override
		public void doubleClick(DoubleClickEvent event) {
			System.out.println(event.getSelection().toString());
			IStructuredSelection selection = (IStructuredSelection)event.getSelection();
			System.out.println(selection.getFirstElement());
			if(((String)selection.getFirstElement()).equals(ERROR_INTENSITY)){
				this.executeErrorIntensity();
				
			} else if(((String)selection.getFirstElement()).equals(CHANGE_COUPLING)){
				try {
					ChangeCouplingView modelView = (ChangeCouplingView) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(ChangeCouplingView.ID);
					modelView.openChangeCouplingView(null);
				} catch (PartInitException e) {
					System.out.println("View could not be opened.");
				}
				
			} else if(((String)selection.getFirstElement()).equals(LINES_OF_CODE)){
				ArchitectureAnalysisView view = (ArchitectureAnalysisView)PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(ArchitectureAnalysisView.ID);
				/* FIXME NullpointerExceptions when the ArchitectureAnalysis view is not activated. */
				DiagramEditPart edit = (DiagramEditPart) view.getViewer().getContents();
				View elementView = (View)edit.getModel();
				List<DiagramComponent> components = new DiagramComponentProvider((Diagram)elementView.getElement()).getDiagramComponents();
				IHeatMap metric = new LinesOfCodeHeatMap(components);
				new ApplyHeatMapCommand(view.getViewer(), metric).execute();
				
			} else if(((String)selection.getFirstElement()).equals(CYCLOMATIC_CODE_COMPLEXITY)){
				ArchitectureAnalysisView view = (ArchitectureAnalysisView)PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(ArchitectureAnalysisView.ID);
				DiagramEditPart edit = (DiagramEditPart) view.getViewer().getContents();
				View elementView = (View)edit.getModel();
				List<DiagramComponent> components = new DiagramComponentProvider((Diagram)elementView.getElement()).getDiagramComponents();
				IHeatMap metric = new CyclomaticComplexityHeatMap(components);
				new ApplyHeatMapCommand(view.getViewer(), metric).execute();
				
			} else{
				this.executeFanInOut();
			}
		}
		
		
		
		private void executeErrorIntensity(){
			final Shell dialog = new Shell(shell, SWT.DIALOG_TRIM);
			dialog.setLayout(new GridLayout(2, false));

			Label label = new Label(dialog, SWT.NONE);
			label.setText("Select the Startdate");
			Label label2 = new Label(dialog, SWT.NONE);
			label2.setText("Select the Enddate");

			final DateTime calendar = new DateTime(dialog, SWT.CALENDAR	| SWT.BORDER);
			final DateTime calendar2 = new DateTime(dialog, SWT.CALENDAR | SWT.BORDER);

			Label label3 = new Label(dialog, SWT.NONE);
			label3.setText("Issue Type");
			new Label(dialog, SWT.NONE);
			
			final Combo combo = new Combo(dialog, SWT.MULTI | SWT.BORDER);
			combo.add("Defect");
			combo.add("Improvement");
			combo.add("New Function");
			combo.add("Verify");
			combo.add("Integration");
			combo.add("Merge");
			combo.add("Functional Removal");
			
			Button ok = new Button(dialog, SWT.PUSH);
			ok.setText("OK");
			ok.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
			ok.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					ArchitectureAnalysisView view = (ArchitectureAnalysisView)PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(ArchitectureAnalysisView.ID);
					DiagramEditPart edit = (DiagramEditPart) view.getViewer().getContents();
					View elementView = (View)edit.getModel();
					List<DiagramComponent> components = new DiagramComponentProvider((Diagram)elementView.getElement()).getDiagramComponents();
					IHeatMap metric = new IssueTypeHeatMap(components, calendar.getYear()+"/"+(calendar.getMonth()+1)+"/"+calendar.getDay() , calendar2.getYear()+"/"+(calendar2.getMonth()+ 1)+"/"+ calendar2.getDay(), combo.getText());
					dialog.close();
					new ApplyHeatMapCommand(view.getViewer(), metric).execute();
					
				}
			});
			dialog.setDefaultButton(ok);
			dialog.pack();
			dialog.open();
		}
		
		private void executeFanInOut(){
			final Shell dialog = new Shell(shell, SWT.DIALOG_TRIM);
			dialog.setLayout(new GridLayout(2, false));
			Label label = new Label(dialog,SWT.NONE);
			label.setText("Welcome to Fan In and Out");
			dialog.pack();
			dialog.open();
		}
	}
}
